
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/assumptions/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:03 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Assumptions &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="index.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Ask" href="ask.html" />
    <link rel="prev" title="Algebras" href="../algebras.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="ask.html" title="Ask"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../algebras.html" title="Algebras"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Assumptions</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.assumptions">
<span id="assumptions"></span><span id="assumptions-module"></span><h1>Assumptions<a class="headerlink" href="#module-sympy.assumptions" title="Permalink to this headline">¶</a></h1>
<p>A module to implement logical predicates and assumption system.</p>
<section id="predicate">
<h2>Predicate<a class="headerlink" href="#predicate" title="Permalink to this headline">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.assumptions.assume.</span></span><span class="sig-name descname"><span class="pre">Predicate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/assume.py#L225-L356"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>Base class for mathematical predicates. It also serves as a
constructor for undefined predicate objects.</p>
<p class="rubric">Explanation</p>
<p>Predicate is a function that returns a boolean value [1].</p>
<p>Predicate function is object, and it is instance of predicate class.
When a predicate is applied to arguments, <code class="docutils literal notranslate"><span class="pre">AppliedPredicate</span></code>
instance is returned. This merely wraps the argument and remain
unevaluated. To obtain the truth value of applied predicate, use the
function <code class="docutils literal notranslate"><span class="pre">ask</span></code>.</p>
<p>Evaluation of predicate is done by multiple dispatching. You can
register new handler to the predicate to support new types.</p>
<p>Every predicate in SymPy can be accessed via the property of <code class="docutils literal notranslate"><span class="pre">Q</span></code>.
For example, <code class="docutils literal notranslate"><span class="pre">Q.even</span></code> returns the predicate which checks if the
argument is even number.</p>
<p>To define a predicate which can be evaluated, you must subclass this
class, make an instance of it, and register it to <code class="docutils literal notranslate"><span class="pre">Q</span></code>. After then,
dispatch the handler by argument types.</p>
<p>If you directly construct predicate using this class, you will get
<code class="docutils literal notranslate"><span class="pre">UndefinedPredicate</span></code> which cannot be dispatched. This is useful
when you are building boolean expressions which do not need to be
evaluated.</p>
<p class="rubric">Examples</p>
<p>Applying and evaluating to boolean value:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Q</span><span class="p">,</span> <span class="n">ask</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">prime</span><span class="p">(</span><span class="mi">7</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>You can define a new predicate by subclassing and dispatching. Here,
we define a predicate for sexy primes [2] as an example.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Predicate</span><span class="p">,</span> <span class="n">Integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">SexyPrimePredicate</span><span class="p">(</span><span class="n">Predicate</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">name</span> <span class="o">=</span> <span class="s2">&quot;sexyprime&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">sexyprime</span> <span class="o">=</span> <span class="n">SexyPrimePredicate</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nd">@Q</span><span class="o">.</span><span class="n">sexyprime</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">Integer</span><span class="p">)</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">_</span><span class="p">(</span><span class="n">int1</span><span class="p">,</span> <span class="n">int2</span><span class="p">,</span> <span class="n">assumptions</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">args</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">([</span><span class="n">int1</span><span class="p">,</span> <span class="n">int2</span><span class="p">])</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">prime</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="n">assumptions</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="kc">False</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">args</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">-</span> <span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">sexyprime</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">11</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Direct constructing returns <code class="docutils literal notranslate"><span class="pre">UndefinedPredicate</span></code>, which can be
applied but cannot be dispatched.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Predicate</span><span class="p">,</span> <span class="n">Integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">P</span> <span class="o">=</span> <span class="n">Predicate</span><span class="p">(</span><span class="s2">&quot;P&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">P</span><span class="p">)</span>
<span class="go">&lt;class &#39;sympy.assumptions.assume.UndefinedPredicate&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">P</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">Q.P(1)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">P</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Integer</span><span class="p">)(</span><span class="k">lambda</span> <span class="n">expr</span><span class="p">,</span> <span class="n">assump</span><span class="p">:</span> <span class="kc">True</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">TypeError</span>: <span class="n">&lt;class &#39;sympy.assumptions.assume.UndefinedPredicate&#39;&gt; cannot be dispatched.</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r3"><span class="brackets"><a class="fn-backref" href="#id1">R3</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Predicate_(mathematical_logic">https://en.wikipedia.org/wiki/Predicate_(mathematical_logic</a>)</p>
</dd>
<dt class="label" id="r4"><span class="brackets"><a class="fn-backref" href="#id2">R4</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Sexy_prime">https://en.wikipedia.org/wiki/Sexy_prime</a></p>
</dd>
</dl>
<dl class="py method">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">eval</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">assumptions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/assume.py#L341-L352"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>Evaluate <code class="docutils literal notranslate"><span class="pre">self(*args)</span></code> under the given assumptions.</p>
<p>This uses only direct resolution methods, not logical inference.</p>
</dd></dl>

<dl class="py attribute">
<dt class="sig sig-object py">
<span class="sig-name descname"><span class="pre">handler</span></span><em class="property"> <span class="pre">=</span> <span class="pre">&lt;dispatched</span> <span class="pre">AskPredicateHandler&gt;</span></em></dt>
<dd></dd></dl>

<dl class="py method">
<dt class="sig sig-object py">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">register</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">types</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/assume.py#L317-L324"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>Register the signature to the handler.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py">
<em class="property"><span class="pre">classmethod</span> </em><span class="sig-name descname"><span class="pre">register_many</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">types</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/assume.py#L326-L336"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>Register multiple signatures to same handler.</p>
</dd></dl>

</dd></dl>

<dl class="py class">
<dt class="sig sig-object py">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.assumptions.assume.</span></span><span class="sig-name descname"><span class="pre">AppliedPredicate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/assume.py#L81-L172"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>The class of expressions resulting from applying <code class="docutils literal notranslate"><span class="pre">Predicate</span></code> to
the arguments. <code class="docutils literal notranslate"><span class="pre">AppliedPredicate</span></code> merely wraps its argument and
remain unevaluated. To evaluate it, use the <code class="docutils literal notranslate"><span class="pre">ask()</span></code> function.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Q</span><span class="p">,</span> <span class="n">ask</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">Q.integer(1)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">function</span></code> attribute returns the predicate, and the <code class="docutils literal notranslate"><span class="pre">arguments</span></code>
attribute returns the tuple of arguments.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="go">&lt;class &#39;sympy.assumptions.assume.AppliedPredicate&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">function</span>
<span class="go">Q.integer</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">arguments</span>
<span class="go">(1,)</span>
</pre></div>
</div>
<p>Applied predicates can be evaluated to a boolean value with <code class="docutils literal notranslate"><span class="pre">ask</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<dl class="py property">
<dt class="sig sig-object py">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">arg</span></span></dt>
<dd><p>Return the expression used by this assumption.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Q</span><span class="p">,</span> <span class="n">Symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">arg</span>
<span class="go">x + 1</span>
</pre></div>
</div>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">arguments</span></span></dt>
<dd><p>Return the arguments which are applied to the predicate.</p>
</dd></dl>

<dl class="py property">
<dt class="sig sig-object py">
<em class="property"><span class="pre">property</span> </em><span class="sig-name descname"><span class="pre">function</span></span></dt>
<dd><p>Return the predicate.</p>
</dd></dl>

</dd></dl>

</section>
<section id="querying">
<h2>Querying<a class="headerlink" href="#querying" title="Permalink to this headline">¶</a></h2>
<p>Queries are used to ask information about expressions. Main method for this
is <code class="docutils literal notranslate"><span class="pre">ask()</span></code>:</p>
<dl class="py function">
<dt class="sig sig-object py">
<span class="sig-prename descclassname"><span class="pre">sympy.assumptions.ask.</span></span><span class="sig-name descname"><span class="pre">ask</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">proposition</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">assumptions</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">{}</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/assumptions/ask.py#L356-L494"><span class="viewcode-link"><span class="pre">[source]</span></span></a></dt>
<dd><p>Function to evaluate the proposition with assumptions.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>proposition</strong> : Any boolean expression.</p>
<blockquote>
<div><p>Proposition which will be evaluated to boolean value. If this is
not <code class="docutils literal notranslate"><span class="pre">AppliedPredicate</span></code>, it will be wrapped by <code class="docutils literal notranslate"><span class="pre">Q.is_true</span></code>.</p>
</div></blockquote>
<p><strong>assumptions</strong> : Any boolean expression, optional.</p>
<blockquote>
<div><p>Local assumptions to evaluate the <em>proposition</em>.</p>
</div></blockquote>
<p><strong>context</strong> : AssumptionsContext, optional.</p>
<blockquote>
<div><p>Default assumptions to evaluate the <em>proposition</em>. By default,
this is <code class="docutils literal notranslate"><span class="pre">sympy.assumptions.global_assumptions</span></code> variable.</p>
</div></blockquote>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><p><code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
</dd>
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><p><strong>TypeError</strong> : <em>proposition</em> or <em>assumptions</em> is not valid logical expression.</p>
<p><strong>ValueError</strong> : assumptions are inconsistent.</p>
</dd>
</dl>
<p class="rubric">Explanation</p>
<p>This function evaluates the proposition to <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code> if
the truth value can be determined. If not, it returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>It should be discerned from <a class="reference internal" href="refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-func docutils literal notranslate"><span class="pre">refine()</span></code></a> which, when applied to a
proposition, simplifies the argument to symbolic <code class="docutils literal notranslate"><span class="pre">Boolean</span></code> instead of
Python built-in <code class="docutils literal notranslate"><span class="pre">True</span></code>, <code class="docutils literal notranslate"><span class="pre">False</span></code> or <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p><strong>Syntax</strong></p>
<blockquote>
<div><ul class="simple">
<li><dl class="simple">
<dt>ask(proposition)</dt><dd><p>Evaluate the <em>proposition</em> in global assumption context.</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>ask(proposition, assumptions)</dt><dd><p>Evaluate the <em>proposition</em> with respect to <em>assumptions</em> in
global assumption context.</p>
</dd>
</dl>
</li>
</ul>
</div></blockquote>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">ask</span><span class="p">,</span> <span class="n">Q</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.abc</span> <span class="kn">import</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">rational</span><span class="p">(</span><span class="n">pi</span><span class="p">))</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="o">*</span><span class="n">y</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">prime</span><span class="p">(</span><span class="mi">4</span><span class="o">*</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
</div>
<p>If the truth value cannot be determined, <code class="docutils literal notranslate"><span class="pre">None</span></code> will be returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">odd</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="n">x</span><span class="p">)))</span> <span class="c1"># cannot determine unless we know x</span>
<span class="go">None</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">ValueError</span></code> is raised if assumptions are inconsistent.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">integer</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">even</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&amp;</span> <span class="n">Q</span><span class="o">.</span><span class="n">odd</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="gr">ValueError</span>: <span class="n">inconsistent assumptions Q.even(x) &amp; Q.odd(x)</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>Relations in assumptions are not implemented (yet), so the following
will not give a meaningful result.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>It is however a work in progress.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="refine.html#sympy.assumptions.refine.refine" title="sympy.assumptions.refine.refine"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.assumptions.refine.refine</span></code></a></dt><dd><p>Simplification using assumptions. Proposition is not reduced to <code class="docutils literal notranslate"><span class="pre">None</span></code> if the truth value cannot be determined.</p>
</dd>
</dl>
</div>
</dd></dl>

<p><code class="docutils literal notranslate"><span class="pre">ask</span></code>’s optional second argument should be a boolean expression involving
assumptions about objects in <em>expr</em>. Valid values include:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">Q.integer(x)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Q.positive(x)</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">Q.integer(x)</span> <span class="pre">&amp;</span> <span class="pre">Q.positive(x)</span></code></p></li>
<li><p>etc.</p></li>
</ul>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">Q</span></code> is an object holding known predicates.</p>
<p>See documentation for the logic module for a complete list of valid boolean
expressions.</p>
<p>You can also define a context so you don’t have to pass that argument
each time to function <code class="docutils literal notranslate"><span class="pre">ask()</span></code>. This is done by using the assuming context manager
from module sympy.assumptions.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">facts</span> <span class="o">=</span> <span class="n">Q</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">Q</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">assuming</span><span class="p">(</span><span class="o">*</span><span class="n">facts</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">ask</span><span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">positive</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">)))</span>
<span class="go">True</span>
</pre></div>
</div>
</section>
<section id="contents">
<h2>Contents<a class="headerlink" href="#contents" title="Permalink to this headline">¶</a></h2>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="ask.html">Ask</a></li>
<li class="toctree-l1"><a class="reference internal" href="assume.html">Assume</a></li>
<li class="toctree-l1"><a class="reference internal" href="refine.html">Refine</a></li>
<li class="toctree-l1"><a class="reference internal" href="predicates.html">Predicates</a><ul>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#common">Common</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#tautological">Tautological</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#commutative">Commutative</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#calculus">Calculus</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#finite">Finite</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#infinite">Infinite</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#matrix">Matrix</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#symmetric">Symmetric</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#invertible">Invertible</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#orthogonal">Orthogonal</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#unitary">Unitary</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#positive-definite">Positive Definite</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#upper-triangular">Upper triangular</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#lower-triangular">Lower triangular</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#diagonal">Diagonal</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#full-rank">Full rank</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#square">Square</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#integer-elements">Integer elements</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#real-elements">Real elements</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#complex-elements">Complex elements</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#singular">Singular</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#normal">Normal</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#triangular">Triangular</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#unit-triangular">Unit triangular</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#number-theory">Number Theory</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#even">Even</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#odd">Odd</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#prime">Prime</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#composite">Composite</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#order">Order</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#positive">Positive</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#negative">Negative</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#zero">Zero</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#nonzero">Nonzero</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#nonpositive">Nonpositive</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#nonnegative">Nonnegative</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="predicates.html#sets">Sets</a><ul>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#integer">Integer</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#rational">Rational</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#irrational">Irrational</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#real">Real</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#extended-real">Extended real</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#hermitian">Hermitian</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#complex">Complex</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#imaginary">Imaginary</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#antihermitian">Antihermitian</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#algebraic">Algebraic</a></li>
<li class="toctree-l3"><a class="reference internal" href="predicates.html#transcendental">Transcendental</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</section>
<section id="performance-improvements">
<h2>Performance improvements<a class="headerlink" href="#performance-improvements" title="Permalink to this headline">¶</a></h2>
<p>On queries that involve symbolic coefficients, logical inference is used. Work on
improving satisfiable function (sympy.logic.inference.satisfiable) should result
in notable speed improvements.</p>
<p>Logic inference used in one ask could be used to speed up further queries, and
current system does not take advantage of this. For example, a truth maintenance
system (<a class="reference external" href="https://en.wikipedia.org/wiki/Truth_maintenance_system">https://en.wikipedia.org/wiki/Truth_maintenance_system</a>) could be implemented.</p>
</section>
<section id="misc">
<h2>Misc<a class="headerlink" href="#misc" title="Permalink to this headline">¶</a></h2>
<p>You can find more examples in the in the form of test under directory
sympy/assumptions/tests/</p>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Assumptions</a><ul>
<li><a class="reference internal" href="#predicate">Predicate</a></li>
<li><a class="reference internal" href="#querying">Querying</a></li>
<li><a class="reference internal" href="#contents">Contents</a></li>
<li><a class="reference internal" href="#performance-improvements">Performance improvements</a></li>
<li><a class="reference internal" href="#misc">Misc</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="../algebras.html"
                        title="previous chapter">Algebras</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="ask.html"
                        title="next chapter">Ask</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/assumptions/index.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="ask.html" title="Ask"
             >next</a> |</li>
        <li class="right" >
          <a href="../algebras.html" title="Algebras"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Assumptions</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/assumptions/index.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:04 GMT -->
</html>